const http=require("http");
const fs=require("fs")
const path=require('path');
const tool=require("./tool.js")
http.createServer((req,res)=>{
    // 1.获取地址
    let pathname=new URL(req.url,"http://localhost").pathname;
    // 默认/index.html
    pathname=pathname=='/'?'/index.html':pathname;
    // 获取后缀
    let extname=path.extname(pathname);
    // 2.读取文件
    if(pathname!='/favicon.ico'){
        console.log(tool.getMimeJson(extname))
        fs.readFile("./static"+pathname,(err,data)=>{
            if(err){
                res.writeHead(404,{'Content-Type':"text/html;charset=UTF8"})
                res.end(err.toString())
                return
            }
            res.writeHead(200,{'Content-Type':tool.getMimeJson(extname)+";charset=UTF8"})
            res.end(data)
        })
    }
  
}).listen(3000)